package dhm.com.dhmshop.updata.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.ShopAdapter;
import dhm.com.dhmshop.base.LazyLoadFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetMyShopinfo;
import dhm.com.dhmshop.entity.GetShopDetailBean;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.framework.module.home.activity.SearchActivity;
import dhm.com.dhmshop.updata.activity.CollectiongoodsActivity;
import dhm.com.dhmshop.updata.activity.CollectionshopActivity;
import dhm.com.dhmshop.updata.activity.ComplaintActivity;
import dhm.com.dhmshop.updata.activity.FunctionActivity;
import dhm.com.dhmshop.updata.activity.InvoiceActivity;
import dhm.com.dhmshop.updata.activity.MessageActivity;
import dhm.com.dhmshop.updata.activity.SalelistActivity;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.mine.AddressActivity;
import dhm.com.dhmshop.view.mine.MyfootActivity;
import dhm.com.dhmshop.view.mine.MyinfoActivity;
import dhm.com.dhmshop.view.mine.MyorderActivity;
import dhm.com.dhmshop.view.mine.SettingActivity;


/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class ShopDetailAllFragment extends LazyLoadFragment implements LoginContract.IView {

    Unbinder unbinder;
    @BindView(R.id.xiao)
    TextView xiao;
    @BindView(R.id.price)
    CheckBox price;
    @BindView(R.id.priceup)
    ImageView priceup;
    @BindView(R.id.pricedown)
    ImageView pricedown;
    @BindView(R.id.renqi)
    TextView renqi;
    @BindView(R.id.time)
    CheckBox time;
    @BindView(R.id.timeup)
    ImageView timeup;
    @BindView(R.id.timedown)
    ImageView timedown;
    Map<String,String> map;
    @BindView(R.id.recycle_star)
    XRecyclerView recycleStar;
    private View rootView;
    private PressenterImpl pressenter;
    private final GetShopDetailBean shopDetail;
    private ShopAdapter shopAdapter;
    private String uid;
    private String userType;

    private List<GetMyShopinfo.DataBean> allSearch;

    protected void initView() {
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(getActivity(), "uid");
        GridLayoutManager manager=new GridLayoutManager(getActivity(),2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleStar.setLayoutManager(manager);

        recycleStar.setLoadingMoreEnabled(true);
        recycleStar.setPullRefreshEnabled(true);
        recycleStar.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                offset = 1;
                map.put("page", offset + "");
                pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
            }

            @Override
            public void onLoadMore() {
                map.put("page", offset + "");
                pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
            }
        });


        initData();
    }

    public ShopDetailAllFragment(GetShopDetailBean shopDetail) {
        this.shopDetail = shopDetail;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    protected void initData() {
        offset = 1;
        map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("shop_id", shopDetail.getData().getShopId()+"");
        map.put("page", offset + "");
        pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fregment_shopdetail_all, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected int setContentView() {
        return R.layout.fregment_shopdetail_all;
    }

    @SuppressLint("NewApi")
    @Override
    protected void lazyLoad() {

    }

    private int offset;
    @OnClick({R.id.xiao, R.id.lin_price, R.id.renqi, R.id.lin_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xiao:
//                销量
                offset=1;
                xiao.setTextColor(getResources().getColor(R.color.main));
                renqi.setTextColor(getResources().getColor(R.color.mainblack));
                price.setChecked(false);
                time.setChecked(false);
                priceup.setImageResource(R.mipmap.priceupn);
                pricedown.setImageResource(R.mipmap.pricedownn);
                price.setTextColor(getResources().getColor(R.color.black));
                map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("shop_id", shopDetail.getData().getShopId()+"");
                map.put("msort","2");
                map.put("mdesc","1");
                map.put("page", offset + "");
                pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
                break;
            case R.id.lin_price:
//                价格
                offset=1;
                price.setChecked(!price.isChecked());
                renqi.setTextColor(getResources().getColor(R.color.mainblack));
                xiao.setTextColor(getResources().getColor(R.color.mainblack));
                time.setChecked(false);
                if (price.isChecked()){
                    priceup.setImageResource(R.mipmap.priceups);
                    pricedown.setImageResource(R.mipmap.pricedownn);
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("shop_id", shopDetail.getData().getShopId()+"");
                    map.put("msort", "3");
                    map.put("mdesc", "1");
                    map.put("page", offset + "");
                    pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
                }else {
                    priceup.setImageResource(R.mipmap.priceupn);
                    pricedown.setImageResource(R.mipmap.pricedowns);
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("shop_id", shopDetail.getData().getShopId()+"");
                    map.put("msort", "3");
                    map.put("mdesc", "0");
                    map.put("page", offset + "");
                    pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
                }
                price.setTextColor(getResources().getColor(R.color.main));
                break;
            case R.id.renqi:
//                人气
                offset=1;
                xiao.setTextColor(getResources().getColor(R.color.mainblack));
                renqi.setTextColor(getResources().getColor(R.color.main));
                price.setChecked(false);
                time.setChecked(false);
                priceup.setImageResource(R.mipmap.priceupn);
                pricedown.setImageResource(R.mipmap.pricedownn);
                price.setTextColor(getResources().getColor(R.color.black));
                map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("shop_id", shopDetail.getData().getShopId()+"");
                map.put("msort","1");
                map.put("mdesc","1");
                map.put("page", offset + "");
                pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);

                break;
            case R.id.lin_time:
//                上架时间
                offset=1;
                renqi.setTextColor(getResources().getColor(R.color.mainblack));
                xiao.setTextColor(getResources().getColor(R.color.mainblack));
                price.setTextColor(getResources().getColor(R.color.mainblack));
                time.setTextColor(getResources().getColor(R.color.main));
                price.setChecked(false);
                time.setChecked(false);
                if (price.isChecked()){
                    timeup.setImageResource(R.mipmap.priceups);
                    timedown.setImageResource(R.mipmap.pricedownn);
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("shop_id", shopDetail.getData().getShopId()+"");
                    map.put("msort", "6");
                    map.put("mdesc", "1");
                    map.put("page", offset + "");
                    pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
                }else {
                    timeup.setImageResource(R.mipmap.priceupn);
                    timedown.setImageResource(R.mipmap.pricedowns);
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("shop_id", shopDetail.getData().getShopId()+"");
                    map.put("msort", "6");
                    map.put("mdesc", "0");
                    map.put("page", offset + "");
                    pressenter.sendMessage(getActivity(), Constant.GetShopGoodsList, map, GetMyShopinfo.class);
                }
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetMyShopinfo) {
            GetMyShopinfo goodSearch = (GetMyShopinfo) data;
            if (goodSearch.getCode() == 1) {
                if (offset == 1) {
                    allSearch = goodSearch.getData();
                    shopAdapter = new ShopAdapter(getActivity());
                    shopAdapter.setaddClick(new ShopAdapter.OnIntentClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(getActivity(), CommodityDetailsActivity.class);
                            intent.putExtra("gid", allSearch.get(posttion).getGoods_id());
                            intent.putExtra("shop_id", allSearch.get(posttion).getShop_id());
                            startActivity(intent);
                        }
                    });
                    recycleStar.setAdapter(shopAdapter);
                } else {
                    allSearch.addAll(goodSearch.getData());
                }
                shopAdapter.setShopList(allSearch);
                offset++;
            } else {
                Toast.makeText(getActivity(), goodSearch.getMessage(), Toast.LENGTH_SHORT).show();
            }
            recycleStar.loadMoreComplete();
            recycleStar.refreshComplete();
        }
    }

    @Override
    public void fail(String error) {

    }
}
